use std::path::PathBuf;
use std::process;
use structopt::StructOpt;

mod err;
mod opt;
mod core;
use self::opt::Opt;
use self::core::read::{load_csv, write_csv};
use self::core::write::relacement_column;

fn main() {
    let opt = Opt::from_args();

    /* 1. 加载原始csv文件 */
    let lines = match load_csv(opt.input) {
        Ok(s)  => s,
        Err(e) => {
            println!("Load source csv file error: {:?}", e);
            process::exit(1);
        }
    };

    /* 2. 替换字段 */
    let lines = match relacement_column(lines, &opt.column, &opt.replacement) {
        Ok(s) => s,
        Err(e) => {
            println!("replace column error: {:?}", e);
            process::exit(1);
        }
    };

    /* 3. 写入新的csv文件 */
    let output_file = opt.output.unwrap_or(PathBuf::from("output/output.csv"));
    match write_csv(&lines, &output_file) {
        Ok(_)  => println!("write success: {}", output_file.to_string_lossy()),
        Err(e) => {
            println!("Main Error: {:?}", e);
            process::exit(1);
        }
    }
}
